unit GroupManagementForm;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, uADStanIntf, uADStanOption,
  uADStanError, uADGUIxIntf, uADPhysIntf, uADStanDef, uADStanPool, uADStanAsync,
  uADPhysManager, uADGUIxFormsWait, uADStanParam, uADDatSManager, uADDAptIntf,
  uADDAptManager, Vcl.Grids, Vcl.DBGrids, Vcl.ExtCtrls, Vcl.DBCtrls, UDF,
  Vcl.StdCtrls, uADCompClient, Data.DB, uADCompDataSet, uADCompGUIx, uADPhysIB,
  uADPhysMySQL;

type
  TfmGroupManagement = class(TForm)
    ADConnection1: TADConnection;
    ADPhysIBDriverLink1: TADPhysIBDriverLink;
    ADGUIxWaitCursor1: TADGUIxWaitCursor;
    ADQuery1: TADQuery;
    dsGroups: TDataSource;
    adtGroups: TADTable;
    btnNew: TButton;
    btnSave: TButton;
    btnDelete: TButton;
    btnClose: TButton;
    Label2: TLabel;
    txtGroupName: TEdit;
    DBGrid1: TDBGrid;
    ADPhysMySQLDriverLink1: TADPhysMySQLDriverLink;
    procedure btnNewClick(Sender: TObject);
    procedure btnCloseClick(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure btnDeleteClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure btnSaveClick(Sender: TObject);
    procedure DBGrid1CellClick(Column: TColumn);
  private
    { Private declarations }
    DBOp: TDBOp;
  public
    { Public declarations }
  end;

var
  fmGroupManagement: TfmGroupManagement;

implementation

{$R *.dfm}

procedure TfmGroupManagement.btnCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfmGroupManagement.btnDeleteClick(Sender: TObject);
begin
  if adtGroups.FieldByName('ID').AsInteger < 3 then
  begin
    ShowMessage('Can''t delete default group!');
    Exit;
  end;

  // Check references before delete
  with ADQuery1 do
  begin
    SQL.Clear;
    Params.Clear;
    SQL.Add('Select * From Users Where GroupID=:ID;');
    Params.ParamByName('ID').AsInteger := adtGroups.FieldByName('ID').AsInteger;
    Open;
    if RecordCount > 0 then
    begin
      // have reference, exit
      ShowMessage('Can''t delete, some users refered to this Group.');
      Exit;
    end;
    txtGroupName.Text := '';
  end;

  // If no reference then delete
  with ADQuery1 do
  begin
    SQL.Clear;
    Params.Clear;
    SQL.Add('Delete From Groups Where ID=:ID;');
    Params.ParamByName('ID').AsInteger := adtGroups.FieldByName('ID').AsInteger;
  end;
  DoQuerySQL(ADConnection1, ADQuery1);
  adtGroups.Refresh;
end;

procedure TfmGroupManagement.btnNewClick(Sender: TObject);
begin
  txtGroupName.Text := '';
  DBOp := opInsert;
end;

procedure TfmGroupManagement.btnSaveClick(Sender: TObject);
begin
  with ADQuery1 do
  begin
    SQL.Clear;
    Params.Clear;
    case DBOp of
      opInsert:
        SQL.Add('Insert Into Groups (GroupName) Values (:GroupName);');
      opUpdate:
        SQL.Add('Update Groups Set GroupName=:GroupName Where ID=:ID;');
    end;
    Params.ParamByName('GroupName').AsString := Trim(txtGroupName.Text);
    if Params.FindParam('ID') <> nil then
      Params.ParamByName('ID').AsInteger := adtGroups.FieldByName('ID').AsInteger;
  end;
  DoQuerySQL(ADConnection1, ADQuery1);
  adtGroups.Refresh;
  txtGroupName.Text := '';
end;

procedure TfmGroupManagement.DBGrid1CellClick(Column: TColumn);
begin
  txtGroupName.Text := adtGroups.FieldByName('GroupName').AsString;
  DBOp := opUpdate;
end;

procedure TfmGroupManagement.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  CanClose := True;
  FreeAndNil(fmGroupManagement);
end;

procedure TfmGroupManagement.FormCreate(Sender: TObject);
var
  ADPhysDriverLink: TADPhysDriverLink;
  DriverID: string;
begin
  DriverID := DBDriverID;
  if DriverID = 'Firebird' then
    ADPhysDriverLink := ADPhysIBDriverLink1;
  if DriverID = 'MySQL' then
    ADPhysDriverLink := ADPhysMySQLDriverLink1;
  SetDBInfo(ADConnection1, ADPhysDriverLink);
  adtGroups.Active := True;
end;

end.
